//生成随机串
function smb_RandomString(len) {
    var randomLen = len || 6;
    var allChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    var maxPos = allChars.length;
    var randomStr = '';
    for (var i = 0; i < randomLen; i++) {
        randomStr += allChars[Math.round(Math.random() * maxPos)];
    }
    return randomStr;
}

/*
 数字前补0
 */
function smb_Fill0(val, length) {
    return smb_FillChar(val, length);
}

function smb_FillChar(val, length, character) {
    if (!character)
        character = "0";
    val = $.trim(val + "").toString();
    length -= val.length;
    for (; length > 0; --length)
        val = character + val;
    return val;
}

/*
 *字符串转化时间格式
 *格式：2017-03-02 12:00:00
 */
function smb_ToDateTime(timeStr) {
    if (!timeStr || timeStr.length == 0)
        return undefined;
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/
    var r = timeStr.match(reg);
    if (r)
        return new Date(r[1], r[3] - 1, r[4]);
    else {
        reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
        r = timeStr.match(reg);
        if (r == null) {
            smb_WriteLog("my.utils.js", timeStr)
            throw '时间格式错误';
        }
        return new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
    }
}


/*
 *type:
 * 0 - 00时00分
 * 1 - 00时00分00秒
 * 2 - 00时
 */
function smb_TimeToString(time, type, isChinese) {
    if (!time)
        return undefined;
    if ( typeof time == 'string')//如果是字符串转换为日期型
        time = smb_ToDateTime(time);
    var str = smb_Fill0(time.getHours(), 2) + ( isChinese ? '时' : ':') + smb_Fill0(time.getMinutes(), 2) + ( isChinese ? '分' : ':') + smb_Fill0(time.getSeconds(), 2) + ( isChinese ? '秒' : '');
    switch(type) {
    case 0:
        str = smb_Fill0(time.getHours(), 2) + ( isChinese ? '时' : ':') + smb_Fill0(time.getMinutes(), 2) + ( isChinese ? '分' : '');
        break;
    case 1:
        break;
    case 2:
        str = smb_Fill0(time.getHours(), 2) + ( isChinese ? '时' : '');
        break;
    }
    return str;
}

/*
 *type:
 * 0 - 0000年00月00日 (周一)
 * 1 - 00年00月00日 (周一)
 * 2 - 00月00日 (周一)
 */
function smb_DateToShortString(date, type, weekFlag, isChinese, sConnStr) {
    if (!sConnStr)
        sConnStr = "-";
    if (!date)
        return undefined;
    if ( typeof date == 'string')//如果是字符串转换为日期型
        date = smb_ToDateTime(date);
    var str = smb_Fill0(date.getMonth() + 1, 2) + ( isChinese ? '月' : sConnStr) + smb_Fill0(date.getDate(), 2) + ( isChinese ? '日' : '');
    switch(type) {
    case 0:
        str = date.getFullYear() + ( isChinese ? '年' : sConnStr) + str;
        break;
    case 1:
        str = date.getFullYear().substr(2, 2) + ( isChinese ? '年' : sConnStr) + str;
        break;
    case 2:
        break;
    }
    if (weekFlag) {
        var iDay = date.getDay();
        switch(iDay) {
        case 0:
            str += " (周日) ";
            break;
        case 1:
            str += " (周一) ";
            break;
        case 2:
            str += " (周二) ";
            break;
        case 3:
            str += " (周三) ";
            break;
        case 4:
            str += " (周四) ";
            break;
        case 5:
            str += " (周五) ";
            break;
        case 6:
            str += " (周六) ";
            break;
        }
    }
    return str;
}

/*
 * 将时间显示转化为字符串
 * 格式：2017-03-02 12:00:00
 */
function smb_DateToString(date, type, weekFlag, shortDate, isChinese) {
    if (!date)
        return undefined;
    if ( typeof date == 'string')//如果是字符串转换为日期型
        date = smb_ToDateTime(date);
    var str = smb_DateToShortString(date, ( shortDate ? 2 : 0), weekFlag, isChinese);
    switch(type) {
    case 0:
        /*2017-03-02*/
        break;
    case 1:
        str = str + ' ' + smb_TimeToString(date, 2, isChinese);
        /*2017-03-02 12*/
        break;
    case 2:
        str = str + ' ' + smb_TimeToString(date, 0, isChinese);
        ;
        break;
    /*2017-03-02 12:30*/
    case 3:
        str = str + ' ' + smb_TimeToString(date, 1, isChinese);
        ;
        break;
    /*2017-03-02 12:00:00*/
    }
    return str;
}


function smb_HtmlEncode(str) {
    var s = "";
    if (!str || str.length == 0)
        return "";
    s = str.replace(/&/g, "&amp;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\n/g, "<br>");
    s = s.replace(/\r/g, "<br>");
    //\r
    return s;
}

function smb_HtmlDecode(str) {
    var s = "";
    if (!str || str.length == 0)
        return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}

function smb_ExcludeSpecial(s) {
    s += '';
    // 去掉转义字符
    s = s.replace(/[-\'\"\\\/\b\f\n\r\t]/g, '');
    // 去掉特殊字符
    s = s.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/);
    return s;
}
